from wx import xrc
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar
import pylab
import matplotlib.mlab as mlab
import numpy as np

from random import random
#from scipy.interpolate import griddata

class DataDisplayer:
    def __init__(self, frame):
        self.frame = frame
        self.contourInited = False
        self.GACurveInited = False

    def writeConsole(self, text):
        """docstring for writeConsole"""
        #self.getControl('console2').AppendText(text+'\n')
        self.getControl('console_textctrl').AppendText(text+'\n')

    def toggleTest(self):
        """docstring for toggle"""
        self.getControl('console_textctrl').Enable(False)

    def deleteContour(self):
        """docstring for deleteContour"""
        self.contour.clear()
        self.canvasContour.draw()

    def initContour(self):
        self.contourInited = True
        self.contour = plt.figure()
        #self.contour = Figure()
        #self.dpi = self.contour.get_dpi()
        #self.contour.set_figheight(240/self.dpi - 0.2)
        #self.contour.set_figwidth(350/self.dpi)
        self.color = '#FFFFFF'
        self.contour.set_facecolor(self.color)
        self.contour.set_edgecolor(self.color)

        #self.axesContour = self.contour.add_subplot(111)
        #self.axesContour.set_title('Data', size=12)
        #self.axesContour.set_axis_bgcolor('black')
        self.canvasContour = FigCanvas(self.getControl('contour_panel'), -1, self.contour)
        self.axesContour = self.contour.add_subplot(111)
        self.axesContour.set_title('Data', size=12)
        #self.CS = plt.contour(x, y, z)
        #self.axesContour = self.contour.add_subplot(121)
        #plt.colorbar()
        self.firstTime = True

    def drawContour(self, x, y, z):
        if self.contourInited:
                #self.contour.clear()
            #self.axesContour = self.contour.add_subplot(111)
            #self.axesContour.set_title('Data', size=12)

            CS = plt.contour(x, y, z)
            #if self.firstTime:
                #plt.colorbar()
                #self.firstTime = False
            #plt.colorbar()
            plt.scatter(x,y,marker='o',c='b',s=5)
            plt.xlabel('c', size=12)
            plt.ylabel('g')
            #CS = plt.contour(x, y, z, linewidths=0.5, color='k')
            #plt.show()

            #print x
            #print y
            #print z
            #CS = plt.contourf(x,y,z,15,cmap=plt.cm.jet)
            #plt.colorbar()
            #plt.scatter(x,y,marker='o',c='b',s=5)
            #plt.xlabel('c')
            #plt.ylabel('g')
            #plt.show()

            self.axesContour.clabel(CS, inline=1, fontsize=10)

            self.canvasContour.draw()


    def deleteGaCurve(self):
        """docstring for deleteGaCurve"""
        self.curve.clear()
        self.canvasGACurve.draw()

    def initGACurve(self):
        self.GACurveInited = True
        #self.dpi = 100
        #self.curve = Figure((4.0,4.0), dpi=self.dpi)
        self.curve = Figure()
        #self.curve = plt.figure()
        #self.dpi = self.curve.get_dpi()
        #self.curve.set_figheight(240/self.dpi - 0.2)
        #self.curve.set_figwidth(350/self.dpi)
        self.color = '#FFFFFF'
        self.curve.set_facecolor(self.color)
        self.curve.set_edgecolor(self.color)

        self.axesGACurve = self.curve.add_subplot(111)
        self.axesGACurve.set_axis_bgcolor('black')
        self.axesGACurve.grid(True, color='gray')
        self.axesGACurve.set_title('GA Curve', size=12)
        #self.axesGACurve.set_xbound(lower = 0, upper = 14)
        #self.axesGACurve.set_ybound(lower = 0, upper = 1)
        pylab.setp(self.axesGACurve.get_xticklabels(), fontsize=10)
        pylab.setp(self.axesGACurve.get_yticklabels(), fontsize=10)

        self.data = []
        self.plot_data = self.axesGACurve.plot(
            self.data,
            linewidth=1,
            color=(1, 1, 0),
            )[0]

        self.canvasGACurve = FigCanvas(self.getControl('ga_curve_panel'), -1, self.curve)
        #self.axesGACurve.set_xbound(lower = 0, upper = 5)
        #self.axesGACurve.set_ybound(lower = 0, upper = 1)
        #for i in range(11):
            #self.data.append(random())
            #self.drawGACurve(0,0)

        #self.axesGACurve.plot(np.arange(len(self.data)), np.array(self.data), linewidth=1, color=(1,1,0))
        #self.canvas.draw()
    def drawGACurve(self, generation, rate):
        """docstring for drawGACurve"""
        if self.GACurveInited:
            self.data.append(rate)
            #self.data.append(random())
            #print self.data
            if len(self.data) > 9:
                self.axesGACurve.set_xbound(lower = len(self.data)-10, upper = len(self.data))
            else:
                self.axesGACurve.set_xbound(lower = 0, upper = 11)

            #print "setting xbound"
            #self.axesGACurve.set_xbound(lower = 0, upper = 5)
            self.plot_data.set_xdata(np.arange(len(self.data)))
            self.plot_data.set_ydata(np.array(self.data))
            #self.axesGACurve.plot(np.arange(len(self.data)), np.array(self.data), linewidth=1, color=(1,1,0))
            self.canvasGACurve.draw()

    def getControl(self, xmlid):
        '''Retrieves the given control (within a frame) by its xmlid'''
        control = self.frame.FindWindowById(xrc.XRCID(xmlid))
        assert control != None, 'Programming error: a control with xml id ' + xmlid + ' was not found.'
        return control


    def getFrame(self):
        """docstring for getFrame"""
        return self.frame
